<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Post extends Admin_Controller{
	
	public function __construct(){
		parent::__construct();
		$this->loadData('controllerName',$this->router->fetch_class());
	}
	
	public function index(){
		redirect('administrator/post/manager');
	}
	
	//Quan ly bai viet
	public function manager(){
		$per_page = 20;
		$num_link = 7;
		$this->load->helper('form');
		$this->load->library('form_validation');
		$this->loadScript(base_url('js/admin/jquery.modify.js'));
		$this->loadScript(base_url('js/admin/jquery.update.status.js'));
		$this->headTitle('Quản lý bài viết - Admin Panel');
		$tpid = $this->input->get('tpid',true);
		$this->load->model('TypePost_Model');
		$typepost = $this->TypePost_Model->getByTpIdTypePost($tpid);
		if($typepost){
			$currentPage = $this->input->get('page',true);
			$currentPage = (!empty($currentPage))?$currentPage:'1';
			$this->load->model('Posts_Model');
			$this->loadData('typepost',$typepost);
			$this->loadData('listtypepost',$this->TypePost_Model->getAllTypePost());
			$countpost = $this->Posts_Model->countPostByTpId($tpid);
			$this->loadData('countpost',$countpost);
			$pagingConfig = $this->init_pagination('administrator/post/manager?tpid='.$tpid, $countpost, $per_page, $num_link);
			$this->loadData('paging', $this->pagination->create_links());
			$this->loadData('listposts', $this->Posts_Model->getPostLimitByTpId($tpid, (($currentPage-1) * $pagingConfig['per_page']), $pagingConfig['per_page']));
			
			if($this->input->post()){
				if($this->form_validation->run('formsearch') == true){
					$keysearch = trim($this->input->post('keysearch', true));
					$fieldsearch = trim($this->input->post('field_search', true));
					$this->loadData('textkeysearch', $keysearch);
					$this->loadData('paging','');
					$this->loadData('listposts', $this->Posts_Model->searchPost($keysearch, $fieldsearch, $tpid));
				}

			}
			$this->loadView('administrator/post/manager');
		}else{
			$firsttypepost = $this->TypePost_Model->getFirstTypePost();
			redirect('administrator/post/manager?tpid='.$firsttypepost->TpId);
		}
	}
	
	//Doi trang thai bai viet
	public function change_status(){
		if($this->input->post()){
			$key = $this->input->post('key', true);
			$status = $this->input->post('status', true);
			$this->load->model('Posts_Model');
			$changeStatus = $this->Posts_Model->changeStatusPost($key, $status);
			if($changeStatus){
				$json['status'] = $changeStatus;
				$json['success'] = true;
			}else{
				$json['success'] = false;
			}
			echo json_encode($json);
		}
	}
	
	//Xoa bai viet
	public function delete(){
		if($this->input->post()){
			$key = $this->input->post('key', true);
			$this->load->model('Posts_Model');
			$post = $this->Posts_Model->getPostByPostId($key);
			if($this->Posts_Model->deletePost($key)){
				if(file_exists($post->Image)){
					unlink($post->Image);
				}
				$json["success"] = true;
			}else{
				$json["success"] = false;
			}
			echo json_encode($json);
		}
	}
	//Them bai viet
	public function add(){
		$this->load->helper('form');
		$this->load->library('form_validation');
		$errorupload = "";
		
		$this->load->model('TypePost_Model');
		$listtypepost = $this->TypePost_Model->getAllTypePost();
		$this->loadData('listtypepost',$listtypepost);
		if($this->input->post()){
			if($this->form_validation->run('post') == true){							
				//if($_FILES['Image']['error'] == 4){
				$path = 'uploads/posts/'.date('m-Y').'/';
				if (!file_exists($path)) {
					mkdir($path, 0777, true);
				}
				$upload = $this->upload_image_process('Image', $path);
				if(isset($upload['error_upload'])){
					//$url_image = null;	
					$errorupload = "<p>".$upload['error_upload']."</p>";
				}else{
					$this->load->model('Posts_Model');	
					$url_image = $upload['image_full_url'];
					$this->Posts_Model->insertPost($url_image);
					redirect('administrator/post/manager?tpid='.$this->input->post('typepost', true));	
				}
			}		
		}
		$this->loadData('errorupload',$errorupload);
		$this->loadStylesheet(base_url('plugins/chosen/chosen.css'));
		$this->loadScript(base_url('plugins/chosen/chosen.jquery.min.js'));
		$this->loadScript(base_url('plugins/ckeditor/ckeditor.js'));
		$this->loadScript(base_url('js/admin/jquery.fixedListcontrol.js'));
		$this->loadScript(base_url('js/jquery.customFile.js'));
		$this->loadScript(base_url('js/admin/jquery.image.js'));
		$this->headTitle('Thêm bài viết - Admin Panel');
		$this->loadView('administrator/post/add');
	}
	
	//Sua bai viet
	public function modify(){
		$this->load->helper('form');
		$this->load->library('form_validation');
		$this->load->model('Posts_Model');	
		$postid = $this->input->get('postid', true);
		$post = $this->Posts_Model->getPostByPostId($postid);
		if($post){
			$errorupload = "";
			if($this->input->post()){
				if($this->form_validation->run('post') == true){	
					$url_image = $this->input->post('hidden_image', true);						
					if($_FILES['Image']['error'] != 4){
						$path = 'uploads/posts/'.date('m-Y').'/';
						if (!file_exists($path)) {
							mkdir($path, 0777, true);
						}
						$upload = $this->upload_image_process('Image', $path);
						if(isset($upload['error_upload'])){
							$errorupload = "<p>".$upload['error_upload']."</p>";
						}else{	
							if(file_exists($url_image)){
								unlink($url_image);
							}
							$url_image = $upload['image_full_url'];	
						}
					}
					$this->Posts_Model->updatePost($postid, $url_image);
					redirect('administrator/post/manager?tpid='.$this->input->post('typepost', true));
				}		
			}
			$this->load->model('TypePost_Model');
			$listtypepost = $this->TypePost_Model->getAllTypePost();
			$this->loadData('listtypepost',$listtypepost);
			$this->loadData('post', $post);
			$this->loadData('errorupload',$errorupload);
			$this->loadStylesheet(base_url('plugins/chosen/chosen.css'));
			$this->loadScript(base_url('plugins/chosen/chosen.jquery.min.js'));
			$this->loadScript(base_url('plugins/ckeditor/ckeditor.js'));
			$this->loadScript(base_url('js/admin/jquery.fixedListcontrol.js'));
			$this->loadScript(base_url('js/jquery.customFile.js'));
			$this->loadScript(base_url('js/admin/jquery.image.js'));
			$this->headTitle('Sửa bài viết - Admin Panel');
			$this->loadView('administrator/post/modify');
		}else{
			redirect('administrator/post/manager');
		}
	}
}